package qx.hyj.petrescue.VO;


import io.swagger.v3.oas.annotations.media.Schema;
import qx.hyj.petrescue.exception.UnknowExceptionEnum;

import java.util.Objects;

/**
 * ^_^ author: LaoYe
 * ^_^ date: 2024.07.22
 * 返回结构体
 */
@Schema(title = "响应结构体", description = "http的body的二次封装响应结构体")
public class Result<T> {

    @Schema(description = "状态码")
    private int code;

    @Schema(description = "消息")
    private String message;

    @Schema(description = "是否成功")
    private boolean success;

    @Schema(description = "内容")
    private T data;

    public Result(int code, boolean success, T data) {
        this(code, "", success, data);
    }

    public Result(UnknowExceptionEnum status, boolean success, T data) {
        this(status.getCode(), status.getMsg(), success, data);
    }

    public Result(int code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    public static <T> Result<T> success() {
        return success(UnknowExceptionEnum.SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return success(UnknowExceptionEnum.SUCCESS, data);
    }

    public static <T> Result<T> success(int code, T data) {
        return success(code, "", data);
    }

    public static <T> Result<T> success(UnknowExceptionEnum status, T data) {
        return success(status.getCode(), status.getMsg(), data);
    }

    public static <T> Result<T> success(int code, String message, T data) {
        return new Result<>(code, message, true, data);
    }

    public static <T> Result<T> failure() {
        return failure(UnknowExceptionEnum.NULL_POINT_ERROR);
    }

    public static <T> Result<T> failure(int code) {
        return failure(code, "");
    }

    public static <T> Result<T> failure(UnknowExceptionEnum status) {
        return failure(status.getCode(), status.getMsg());
    }

    public static <T> Result<T> failure(int code, String message) {
        return new Result<>(code, message, false, null);
    }

    public static <T> Result<T> of(int code, boolean success, T data) {
        return of(code, "", success, data);
    }

    public static <T> Result<T> of(UnknowExceptionEnum status, boolean success, T data) {
        return of(status.getCode(), status.getMsg(), success, data);
    }

    public static <T> Result<T> of(int code, String message, boolean success, T data) {
        return new Result<>(code, message, success, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Result<?> result = (Result<?>) o;
        return code == result.code && success == result.success && Objects.equals(message, result.message) && Objects.equals(data, result.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, message, success, data);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", success=" + success +
                ", data=" + data +
                '}';
    }

}
